/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uni.lpoo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import uni.lpoo.tool.Conexion;
import uni.lpoo.dto.ArticuloDTO;

/**
 *
 * @author Administrador
 */
public class ArticuloDAO {

    //Retorna la lista de articulos
    public ArrayList listArticulo() throws Exception {
        ArrayList list = null;
        String sql = "Select * from Articulo";
        String driver = "org.gjt.mysql.driver";
        String url = "jdbc:mysql://localhost/XE";
        String user = "root";
        String pass = "root";
        Conexion conex = new Conexion(driver, url, user, pass);
        Connection cn = conex.getConnectionX();
        Statement stmt = cn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            ArticuloDTO bean = new ArticuloDTO();
            bean.setCodigo(rs.getInt(1));
            bean.setNomArticulo(rs.getString(2));
            bean.setPrecio(rs.getDouble(3));
            bean.setStock(rs.getInt(4));
            list.add(bean);
        }

        return list;
    }

    public int insertArticulo(ArticuloDTO articulo) throws Exception {
        ArrayList list = null;
        String sql = "Insert into Articulo values(?,?,?,?)";
        String driver = "oracle.jdbc.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String user = "fiis";
        String pass = "java";
        Conexion conex = new Conexion(driver, url, user, pass);
        Connection cn = conex.getConnectionX();
        PreparedStatement pstmt = cn.prepareStatement(sql);
        pstmt.setInt(1, articulo.getCodigo());
        pstmt.setString(2, articulo.getNomArticulo());
        pstmt.setDouble(3, articulo.getPrecio());
        pstmt.setInt(4, articulo.getStock());
        int n = pstmt.executeUpdate();
        //stmt.executeUpdate(s);
        return n;
    }

    public int DeleteArticulo(int cod) throws Exception {

        ArrayList list = null;
        String sql = "Delete from Articulo where codigo=?";
        String driver = "oracle.jdbc.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String user = "fiis";
        String pass = "java";
        Conexion conex = new Conexion(driver, url, user, pass);
        Connection cn = conex.getConnectionX();
        PreparedStatement pstmt = cn.prepareStatement(sql);
        pstmt.setInt(1, cod);
        int n = pstmt.executeUpdate();
        return n;
    }
}
